//
//  UITabBar+Extension.m
//  WIFI
//
//  Created by macbook on 2023/6/28.
//

#import "UITabBar+Extension.h"

#define TabbarItemNums 5.0

@implementation UITabBar (Extension)

//显示红点
- (void)showBadgeOnItmIndex:(int)index count:(NSInteger)count {
    [self removeBadgeOnItemIndex:index];
    //新建小红点
    UIView *bview = [[UIView alloc]init];
    bview.tag = 888+index;
    bview.layer.cornerRadius = 8.0f;
    bview.clipsToBounds = YES;
    bview.backgroundColor = [UIColor redColor];
    
    CGRect tabFram = self.frame;
    float percentX = (index+0.6)/TabbarItemNums;
    CGFloat x = ceilf(percentX*tabFram.size.width);
    CGFloat y = ceilf(0.1*tabFram.size.height);
    if (count > 10) {
        bview.frame = CGRectMake(x-5, y-5, 21, 16);
    }else {
        bview.frame = CGRectMake(x-5, y-5, 16, 16);
    }
 
    UILabel *bLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, bview.width, bview.height)];
    if (count > 99) {
        bLabel.text = @"99+";
    }else {
        bLabel.text = [NSString stringWithFormat:@"%ld",(long)count];
    }
    bLabel.font = FONT(FontWeightStyleRegular, 10);
    bLabel.textAlignment = NSTextAlignmentCenter;
    bLabel.textColor = ColorHex(@"#FFFFFF");
    bLabel.tag = 999+index;
    bLabel.backgroundColor = [UIColor redColor];
    [bview addSubview:bLabel];
    
    [self addSubview:bview];
    [self bringSubviewToFront:bview];
}

- (void)showBadgeOnItmIndex:(int)index{
 [self removeBadgeOnItemIndex:index];
 //新建小红点
 UIView *bview = [[UIView alloc]init];
 bview.tag = 888+index;
 bview.layer.cornerRadius = 5;
 bview.clipsToBounds = YES;
 bview.backgroundColor = [UIColor redColor];
 CGRect tabFram = self.frame;

 float percentX = (index+0.6)/TabbarItemNums;
 CGFloat x = ceilf(percentX*tabFram.size.width);
 CGFloat y = ceilf(0.1*tabFram.size.height);
 bview.frame = CGRectMake(x, y, 10, 10);
 [self addSubview:bview];
 [self bringSubviewToFront:bview];
}

//隐藏红点
-(void)hideBadgeOnItemIndex:(int)index{
    [self removeBadgeOnItemIndex:index];
}
//移除控件
- (void)removeBadgeOnItemIndex:(int)index{
    for (UIView*subView in self.subviews) {
        if (subView.tag == 888+index) {
            [subView removeFromSuperview];
        }
        
        if (subView.tag == 999+index) {
            [subView removeFromSuperview];
        }
    }
}

@end
